package com.woniu.exceptions;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.woniu.entity.ResponseEntity;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.security.auth.login.LoginException;
import javax.servlet.ServletException;

/**
 * @author:陈涵
 * @Do: 我们处理全局异常的，不用急，理解即可，以后公司有自己实现的全局异常，百度也有
 * @date: 2022/9/7 7:49
 */
@RestControllerAdvice
public class GlobalExceptionControllerAdvice {
    /**
     * shiro相关的异常：网上还有很多其他异常处理方案
     * ChangeLog : 1. 创建 (22/09/06/0006 16:26 [马宇航]);
     * @param e
     * @return com.woniuxy.entity.ResponseEntity
     */
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ShiroException.class)
    public ResponseEntity handler401(ShiroException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage(),null);
    }
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseEntity handler401(UnauthorizedException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage(),null);
    }

    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthenticatedException.class)
    public ResponseEntity handlerUnauthorizedException(UnauthenticatedException unauthenticatedException){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),unauthenticatedException.getMessage(),null);
    }
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity handlerUnauthorizedException(AuthenticationException e){
        return new ResponseEntity(HttpStatus.UNAUTHORIZED.value(),e.getMessage(),null);
    }
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(JWTVerificationException.class)
    public ResponseEntity handlerUnauthorizedException(JWTVerificationException e){
        return new ResponseEntity(HttpStatus.FORBIDDEN.value(),e.getMessage(),null);
    }
    //封装响应给前端状态码
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(LoginException.class)
    public ResponseEntity loginException(Throwable e){
        return new ResponseEntity(HttpStatus.FORBIDDEN.value(),e.getMessage(),null);
    }
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public ResponseEntity globalException(Throwable e){
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(),e.getMessage(),null);
    }
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ServletException.class)
    public ResponseEntity globalException(ServletException e){
        return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(),e.getMessage(),null);
    }
}
